﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataContexts.Supplier;

public partial class Admin_Supplier_Default : System.Web.UI.Page
{
    SupplierDataContext supplierContext = new SupplierDataContext();

    protected void Page_Load(object sender, EventArgs e)
    {
        ((BaseMasterPage)this.Master).CurrentPage = PageConfig.Pages.AdminSupplierDefault;
        if (!((BaseMasterPage)this.Master).AuthorizeCurrentUser())
            return;

        if (!IsPostBack)
        {

            gvSupplier.DataSource = supplierContext.Suppliers;
            gvSupplier.DataBind();
        }
    }
    protected void gvSupplier_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {

    }
    protected void gvSupplier_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = Convert.ToInt32(gvSupplier.DataKeys[e.RowIndex].Value);
        var suppliers = supplierContext.Suppliers.SingleOrDefault(u => u.SupplierId == id);
        if (suppliers.UserId.HasValue && suppliers.UserId.Value > 0)
        {
            User user = supplierContext.Users.SingleOrDefault(u => u.UserId == suppliers.UserId.Value);
            supplierContext.Users.DeleteOnSubmit(user);
        }
        supplierContext.Suppliers.DeleteOnSubmit(suppliers);
        supplierContext.SubmitChanges();
        Response.Redirect("default.aspx");
    }
    protected void gvSupplier_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ((LinkButton)e.Row.Cells[10].Controls[0]).OnClientClick = "return confirm(\"Are you sure you want to delete '" + e.Row.Cells[0].Text + "' supplier?\");";
        }
    }
}
